package game.editer;

import game.data.ActionData;
import game.editer.view.AlertView;
import hx.events.Event;
import game.editer.config.ActionDataConfig;
import game.editer.view.InputListDataView;
import hx.events.MouseEvent;
import game.editer.ui.CommonButton;
import hx.display.HBox;
import hx.display.ArrayCollection;
import game.data.IRoleData;
import hx.layout.AnchorLayoutData;
import game.editer.ui.LabelListView;

/**
 * 动作面板
 */
class ActionPlane extends BasePanel {
	/**
	 * 项目数据
	 */
	public var projectData(default, set):IRoleData;

	private function set_projectData(value:IRoleData):IRoleData {
		projectData = value;
		if (value != null) {
			actionsListView.data = new ArrayCollection([
				for (action in value.actions) {
					{
						text: action.name + "(" + action.getGroupsKeyString() + ")",
						data: action
					};
				}
			]);
		}
		return value;
	}

	public var actionsListView:LabelListView;

	public function new():Void {
		super("动作列表");
		actionsListView = new LabelListView();
		this.addChild(actionsListView);
		actionsListView.layoutData = new AnchorLayoutData(36, 0, 0, 0);

		// 动作选择器
		var rightBox = new HBox();
		rightBox.gap = 5;
		rightBox.layoutData = AnchorLayoutData.topRight(8, 4);
		this.addChild(rightBox);

		var editBtn = new CommonButton("UIAtlas:edit");
		// editBtn.y = (actionsListView.height - editBtn.height) / 2;
		rightBox.addChild(editBtn);
		editBtn.addEventListener(MouseEvent.CLICK, (e) -> {
			var currentActionData = EditerStage.timeline.currentActionData;
			if (currentActionData == null)
				return;
			var orightName = currentActionData.name;
			var view = new InputListDataView("编辑动作", ActionDataConfig.ATTRIBUTES, 300, 450);
			view.data = currentActionData;
			view.show();
			view.addEventListener(AlertView.BUTTON_CLICK, (e:Event) -> {
				if (e.data == "确定") {
					view.done();
					if (orightName != currentActionData.name) {
						// 说明变更了名称
						EditerStage.world.role.assets.data.actions.remove(orightName);
						this.actionsListView.selectedItem.text = currentActionData.name + "(" + currentActionData.getGroupsKeyString() + ")";
						this.actionsListView.selectedItem = this.actionsListView.selectedItem;
						EditerStage.world.role.assets.data.actions.set(currentActionData.name, currentActionData);
					} else {
						this.actionsListView.selectedItem.text = currentActionData.name + "(" + currentActionData.getGroupsKeyString() + ")";
						this.actionsListView.selectedItem = this.actionsListView.selectedItem;
					}
					EditerStage.world.role.updateMovieClipActionData(currentActionData.name);
					EditerStage.timeline.updateTimelineData();
				}
			});
		});

		var addActionBtn = new CommonButton("UIAtlas:add");
		// addActionBtn.y = (actionsListView.height - addActionBtn.height) / 2;
		rightBox.addChild(addActionBtn);
		addActionBtn.addEventListener(MouseEvent.CLICK, (e) -> {
			var view = new InputListDataView("创建动作", ActionDataConfig.ATTRIBUTES, 300, 450);
			view.data = new ActionData();
			view.show();
			view.addEventListener(AlertView.BUTTON_CLICK, (e:Event) -> {
				if (e.data == "确定") {
					var newActionData = view.done();
					EditerStage.timeline.addActionData(newActionData);
				}
			});
		});

		var removeActionBtn = new CommonButton("UIAtlas:trash");
		// removeActionBtn.y = (actionsListView.height - removeActionBtn.height) / 2;
		rightBox.addChild(removeActionBtn);
		removeActionBtn.addEventListener(MouseEvent.CLICK, (e) -> {
			if (EditerStage.timeline.currentActionData == null)
				return;
			AlertView.showView("删除【" + EditerStage.timeline.currentActionData.name + "】动作", "是否删除该动作，删除后无法恢复。", ["取消", "确定"], (str) -> {
				if (str == "确定") {
					// 删除此动画
					EditerStage.timeline.removeActionData(EditerStage.timeline.currentActionData);
				}
			});
		});
	}
}
